package server;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.Properties;
import java.util.Random;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.iap.Protocol;

public class AutenticaRegistrazione {
	private int port = 465;
	private String host = "smtp.gmail.com";
	private String from = "server.ghprogetto@gmail.com";
	private boolean auth = true;
	private String username = "server.ghprogetto@gmail.com";
	private String password = "GestioneHotel2013";
	private Protocol protocol = Protocol.SMTPS;
	private boolean debug = true;

	public enum Protocol {
		SMTP, SMTPS, TLS
	}

	public String generaCodice() {
		Random rand = new Random();
		StringBuffer s = new StringBuffer();
		s.append("");
		for (int i = 0; i < 7; i++) {
			int c = rand.nextInt(122 - 48) + 48;
			if ((c >= 58 && c <= 64) || (c >= 91 && c <= 96)) {
				i--;
			} else
				s.append((char) c);

		}
		return s.toString();

	}

	public void mandaMail(String to, String subject, String body)
			throws IOException, URISyntaxException, MessagingException {
		try {
			boolean debug = false;

			Properties props = new Properties();
			props.put("mail.smtp.host", host);
			props.put("mail.smtp.port", port);
			switch (protocol) {
			case SMTPS:
				props.put("mail.smtp.ssl.enable", true);
				break;
			case TLS:
				props.put("mail.smtp.starttls.enable", true);
				break;
			}
			Authenticator authenticator = null;
			if (auth) {
				props.put("mail.smtp.auth", true);
				authenticator = new Authenticator() {
					private PasswordAuthentication pa = new PasswordAuthentication(
							username, password);

					@Override
					public PasswordAuthentication getPasswordAuthentication() {
						return pa;
					}
				};
			}
			Session session = Session.getInstance(props, authenticator);
			session.setDebug(debug);
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(from));
			InternetAddress[] address = { new InternetAddress(to) };
			message.setRecipients(Message.RecipientType.TO, address);
			message.setSubject(subject);
			message.setSentDate(new Date());
			message.setText(body);
			Transport.send(message);
		} catch (MessagingException ex) {
			ex.printStackTrace();
		}

	}

	public void mandaMailThread(String to, String subject,
			String body) {
		MandaMailThread mt = new MandaMailThread(this, to, subject, body);
		mt.start();
	}

	class MandaMailThread extends Thread {
		private AutenticaRegistrazione a;
		private String to, subject, body;

		public MandaMailThread(AutenticaRegistrazione a, String to, String subject,
				String body) {
			this.a = a;
			this.to = to;
			this.subject = subject;
			this.body = body;

		}

		public void run() {
			try {
				boolean debug = false;

				Properties props = new Properties();
				props.put("mail.smtp.host", host);
				props.put("mail.smtp.port", port);
				switch (protocol) {
				case SMTPS:
					props.put("mail.smtp.ssl.enable", true);
					break;
				case TLS:
					props.put("mail.smtp.starttls.enable", true);
					break;
				}
				Authenticator authenticator = null;
				if (auth) {
					props.put("mail.smtp.auth", true);
					authenticator = new Authenticator() {
						private PasswordAuthentication pa = new PasswordAuthentication(
								username, password);

						@Override
						public PasswordAuthentication getPasswordAuthentication() {
							return pa;
						}
					};
				}
				Session session = Session.getInstance(props, authenticator);
				session.setDebug(debug);
				MimeMessage message = new MimeMessage(session);
				message.setFrom(new InternetAddress(from));
				InternetAddress[] address = { new InternetAddress(to) };
				message.setRecipients(Message.RecipientType.TO, address);
				message.setSubject(subject);
				message.setSentDate(new Date());
				message.setText(body);
				Transport.send(message);
			} catch (MessagingException ex) {
				ex.printStackTrace();
			}
		}
	}

}
